
/*
 *
 * Copyright (c) 2006 P.J.Leonard
 * 
 * http://www.frinika.com
 * 
 * This file is part of Frinika.
 * 
 * Frinika is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Frinika is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Frinika; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package uk.ac.bath.machine.sane;

import java.awt.Color;
import java.awt.Graphics;
//import java.awt.geom.Point2D;
import java.awt.Point;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import uk.ac.bath.base.Value;

/**
 *
 * @author pjl
 */
public class NetworkPanel extends JPanel {

    HashMap<Value, Point> map = new HashMap<Value, Point>();

    public NetworkPanel() {
        super(true);
    }

    public void setNet(Network net) {
        this.net = net;
        repaint();
    }

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        if (net == null) {
            return;
        }

        System.out.println("Painting");

        int wid = getWidth();
        int height = getHeight();

        int inH = (int) (.8f * height);
        int hidH = (int) (.5f * height);

        int outH = (int) (.2f * height);

        int nMax = Math.max(net.nOut, Math.max(net.nHidden, net.nIn));
        float spaceX = wid / (nMax + 2);

        float mid = wid / 2.0f;
        float midIn = net.nIn / 2.0f;
        float midHid = net.nHidden / 2.0f;
        float midOut = net.nOut / 2.0f;

        Point loc[] = new Point[net.nIn + net.nHidden + net.nOut];

        int ii = 0;
        for (int i = 0; i < net.nIn; i++, ii++) {
            loc[ii] = new Point((int) (mid + (i - midIn) * spaceX), inH);
            map.put(net.x[ii], loc[ii]);
        }


        for (int i = 0; i < net.nHidden; i++, ii++) {
            loc[ii] = new Point((int) (mid + (i - midHid) * spaceX), hidH);
            map.put(net.x[ii], loc[ii]);
        }

        for (int i = 0; i < net.nOut; i++, ii++) {
            loc[i + net.nIn + net.nHidden] = new Point((int) (mid + (i - midOut) * spaceX), outH);
            map.put(net.x[ii], loc[ii]);
        }

        int dd = 20;

        int cnt = 0;
        for (Point p : loc) {

            g.setColor(Color.BLACK);

            g.fillRoundRect(p.x - dd / 2, p.y - dd / 2, dd, dd, dd - 1, dd - 1);

        }

        g.setColor(Color.BLACK);

        for (Neuron n : net.neurons) {
            cnt = 0;
            for (Value in : n.in) {
                Point p1 = map.get(in);
                Point p2 = map.get(n.o);
                System.out.println(in + "  " + n.o + "   " + p1 + " ->" + p2);
                g.drawLine(p1.x, p1.y, p2.x, p2.y);
                g.drawString(String.format(" %3.2f ", n.w[cnt++]), (p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
            }
        }

    }
    Network net;

//    public static void main(String args[]) {
//
//        JFrame frame = new JFrame();
//
//        Context c = new Context();
//
//        ProgressViewer view = new ProgressViewer();
//
//        Population pop = new Population(c, view);
//
//      //  Network net = pop.createRandomNet();
//
////        NetworkPanel panel = new NetworkPanel();
////
////        panel.setNet(net);
//
//        frame.setSize(400, 400);
//        frame.setContentPane(view.view);
//        frame.setVisible(true);
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//    }
}
